package com.ruoyi.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.business.app.bo.AppWithdrawDetailQueryBo;
import com.ruoyi.business.app.vo.WalletAccountDetailVo;
import com.ruoyi.business.bo.HcxWithdrawDetailAddBo;
import com.ruoyi.business.bo.HcxWithdrawDetailEditBo;
import com.ruoyi.business.bo.HcxWithdrawDetailQueryBo;
import com.ruoyi.business.domain.HcxWithdrawDetail;
import com.ruoyi.business.mapper.HcxWithdrawDetailMapper;
import com.ruoyi.business.service.IHcxWithdrawDetailService;
import com.ruoyi.business.vo.HcxWithdrawDetailVo;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.AssertUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 账单明细Service业务层处理
 *
 * @author wsdz
 * @date 2022-08-17
 */
@Service
public class HcxWithdrawDetailServiceImpl extends ServicePlusImpl<HcxWithdrawDetailMapper, HcxWithdrawDetail> implements IHcxWithdrawDetailService {

    @Override
    public HcxWithdrawDetailVo queryById(Long id) {
        return getVoById(id, HcxWithdrawDetailVo.class);
    }

    @Override
    public TableDataInfo<HcxWithdrawDetailVo> queryPageList(HcxWithdrawDetailQueryBo bo) {
        PagePlus<HcxWithdrawDetail, HcxWithdrawDetailVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), HcxWithdrawDetailVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public TableDataInfo<WalletAccountDetailVo> queryAppPageList(AppWithdrawDetailQueryBo bo) {
        PagePlus<HcxWithdrawDetail, WalletAccountDetailVo> result = pageVo(PageUtils.buildPagePlus(), buildAppQueryWrapper(bo), WalletAccountDetailVo.class);
        return PageUtils.buildDataInfo(result);
    }


    @Override
    public List<HcxWithdrawDetailVo> queryList(HcxWithdrawDetailQueryBo bo) {
        return listVo(buildQueryWrapper(bo), HcxWithdrawDetailVo.class);
    }

    private LambdaQueryWrapper<HcxWithdrawDetail> buildQueryWrapper(HcxWithdrawDetailQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HcxWithdrawDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, HcxWithdrawDetail::getUserId, bo.getUserId());
        lqw.eq(StrUtil.isNotBlank(bo.getDetailType()), HcxWithdrawDetail::getDetailType, bo.getDetailType());
        lqw.eq(bo.getAmount() != null, HcxWithdrawDetail::getAmount, bo.getAmount());
        lqw.eq(StrUtil.isNotBlank(bo.getDelFlag()), HcxWithdrawDetail::getDelFlag, bo.getDelFlag());
        lqw.orderByDesc(HcxWithdrawDetail::getCreateTime);
        return lqw;
    }

    private LambdaQueryWrapper<HcxWithdrawDetail> buildAppQueryWrapper(AppWithdrawDetailQueryBo bo) {
        LambdaQueryWrapper<HcxWithdrawDetail> lqw = Wrappers.lambdaQuery();
        lqw.select(HcxWithdrawDetail::getDetailType, HcxWithdrawDetail::getAmount, HcxWithdrawDetail::getCreateTime);
        // 支出
        if(Constants.ONE.equals(bo.getDetailType())){
            lqw.in(HcxWithdrawDetail::getDetailType, Arrays.asList(0,1));
        }else if(Constants.TWO.equals(bo.getDetailType())){
            lqw.in(HcxWithdrawDetail::getDetailType, Arrays.asList(2,3,4));
        }
        lqw.eq(HcxWithdrawDetail::getUserId, SecurityUtils.getUserId())
                .orderByDesc(HcxWithdrawDetail::getCreateTime);
        return lqw;
    }


    @Override
    public Boolean insertByAddBo(HcxWithdrawDetailAddBo bo) {
        HcxWithdrawDetail add = BeanUtil.toBean(bo, HcxWithdrawDetail.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(HcxWithdrawDetailEditBo bo) {
        HcxWithdrawDetail update = BeanUtil.toBean(bo, HcxWithdrawDetail.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(HcxWithdrawDetail entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    @Override
    public String importHcxWithdrawDetail(List<HcxWithdrawDetailVo> hcxWithdrawDetailList) {
        AssertUtils.isTrue(CollectionUtils.isNotEmpty(hcxWithdrawDetailList), "导入数据不能为空");
        int successNum = 0;
        for (HcxWithdrawDetailVo hcxWithdrawDetail : hcxWithdrawDetailList) {
            HcxWithdrawDetail add = BeanUtil.toBean(hcxWithdrawDetail, HcxWithdrawDetail.class);
            save(add);
            successNum++;
        }
        String msg = String.format("导入完成，共计 %d 条数据，成功 %d 条！", hcxWithdrawDetailList.size(), successNum);
        if (successNum != hcxWithdrawDetailList.size()) {
            msg = msg + String.format("失败 %d 条！", hcxWithdrawDetailList.size() - successNum);
        }
        return msg;
    }
}
